﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Configuration;
using System.Data.SqlClient;

namespace CivilleWebRole
{
    public partial class QRCodeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string taskID = Request.QueryString["id"];
            String connectionString = ConfigurationManager
                .ConnectionStrings["CivilleConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "SELECT Code from QRCode WHERE TaskID = '" + taskID + "' AND Status = 'true'";
            connection.Open();
            string code = (string) command.ExecuteScalar();
            connection.Close();

            QRCodeGenerator generator = new QRCodeGenerator();
            Bitmap image = generator.generateQRCode(code);

            MemoryStream stream = new MemoryStream();
            image.Save(stream, ImageFormat.Bmp);
            Byte[] bytes = stream.ToArray();
            Response.BinaryWrite(bytes);
        }
    }
}